<?php
require_once('RegisterView.php');
require_once('RegisterHandler.php');
require_once('Validator.php');

class RegisterController{
		
	
	public function DoControl(\Database $db){
		
		$registerView = new RegisterView();
		$registerHandler = new RegisterHandler($db);  
		$validator = new Validator();
		$message = "";
		
		$message .= $registerView->RegisterUserBox();
		
		if($registerView->TriedToRegister()){
			//Är username redan taget?
			if($registerHandler->UserExist($registerView->GetUserName())){
				 $message .=$registerView->UserExistMessage(); }
			else{
				
				//Validerar indatat
				if ($validator->ValidateUsername($registerView->GetUserName())==TRUE
				&&($validator->ValidatePassword($registerView->GetPassword(), $registerView->GetPassword2())==TRUE)){
						
					$registerHandler->RegisterNewUser($registerView->GetUsername(),$registerView->GetPassword());
					$message .=$registerView->SuccessMessage();
				}
			}
		}
		$message .=$validator->GetValidationError();
		return $message;
		
	}
	
}

